<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { deleteHost, pageHost } from "@/api/vap/shhost.ts";
import { ElMessage } from "element-plus";
import AddForm from "./addForm.vue";

const tableData = ref([]);
const total = ref(0);
const searchForm = reactive({
  ip: "",
  pageNum: 1,
  pageSize: 10
});

const onSearch = () => {
  searchForm.pageNum = 1;
  pageHost(searchForm).then(res => {
    total.value = res.total;
    tableData.value = res.list;
  });
};

const currentPageChange = (value: number) => {
  searchForm.value.pageNum = value;
  pageHost(searchForm).then(res => {
    total.value = res.total;
    tableData.value = res.list;
  });
};

const addFormRef = ref();

const clickAddBtn = () => {
  addFormRef.value.showDialog();
};
const clickDeleteBtn = (row: any) => {
  deleteHost(row.id).then(() => {
    onSearch();
  });
};
const clickConnBtn = (row: any) => {
  console.log(row);
  switch (row.authType) {
    case 1:
      window.open(
        window.location.origin +
          "#/shTerminal?hostId=" +
          row.id +
          "&authType=" +
          row.authType +
          "&username=" +
          "&password="
      );
      break;
    case 2:
      shForm.hostId = row.id;
      dialogVisible.value = true;
      break;
    default:
      ElMessage({
        type: "info",
        message: "未接入的认证方式"
      });
  }
};
const dialogVisible = ref(false);
const shForm = reactive({
  hostId: "",
  username: "",
  password: ""
});
const clickSubConnBtn = () => {
  window.open(
    window.location.origin +
      "#/shTerminal?hostId=" +
      shForm.hostId +
      "&authType=2" +
      "&username=" +
      shForm.username +
      "&password=" +
      shForm.password
  );
};
onMounted(() => {
  onSearch();
});
</script>

<template>
  <div>
    <el-dialog v-model="dialogVisible" title="请输入账号密码" width="500">
      <el-form :model="shForm" label-width="auto" style="max-width: 600px">
        <el-form-item label="账户">
          <el-input v-model="shForm.username" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="shForm.password" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="clickSubConnBtn"> 连接 </el-button>
        </div>
      </template>
    </el-dialog>
    <AddForm ref="addFormRef" @onSearchEvn="onSearch" />
    <el-card>
      <div class="flex justify-between">
        <div>
          <el-input
            v-model="input"
            style="width: 200px"
            placeholder="按 ip 查询主机"
          />
          <el-button type="primary" class="ml-2" @click="onSearch"
            >查询</el-button
          >
        </div>
        <div>
          <el-button type="primary" class="ml-2" @click="clickAddBtn"
            >添加主机</el-button
          >
        </div>
      </div>
    </el-card>
    <el-card>
      <el-table :data="tableData">
        <el-table-column prop="uid" label="主机ID" />
        <el-table-column prop="hostName" label="主机名" />
        <el-table-column prop="ip" label="IP" />
        <el-table-column prop="connPort" label="端口" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column prop="authType" label="认证方式" />
        <el-table-column prop="username" label="默认账户" />
        <el-table-column prop="createdAt" label="创建时间" />
        <el-table-column fixed="right" label="操作" min-width="120">
          <template #default="scope">
            <el-button
              link
              type="primary"
              size="small"
              @click="clickDeleteBtn(scope.row)"
            >
              删除
            </el-button>
            <el-button
              link
              type="primary"
              size="small"
              @click="clickConnBtn(scope.row)"
              >连接</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        @current-change="currentPageChange"
      />
    </el-card>
  </div>
</template>

<style scoped></style>
